import { _decorator, Component, Node, Vec3, Animation, AudioSource, AudioClip } from 'cc';
import { BaseState } from './states/BaseState';
import { IdleState } from './states/IdleState';
import { WanderState } from './states/WanderState';
import { CharacterController } from '../controllers/CharacterController';
import { PathFollower } from '../pathfinding/PathFollower';

const { ccclass, property } = _decorator;

/**
 * AI控制器
 * 
 * 功能：
 * 1. 管理AI状态机
 * 2. 处理AI决策逻辑
 * 3. 控制AI行为执行
 * 4. 管理AI感知系统
 * 
 * 特点：
 * 1. 状态机架构
 * 2. 可配置的决策系统
 * 3. 环境感知能力
 * 4. 路径寻找支持
 */
@ccclass('AIController')
export class AIController extends Component {
    @property(CharacterController)
    private character: CharacterController | null = null;

    @property
    private detectionRadius: number = 10;

    @property
    private moveSpeed: number = 5;

    @property(PathFollower)
    private pathFollower: PathFollower | null = null;

    @property(AudioClip)
    private attackSound: AudioClip | null = null;

    private states: Map<string, BaseState> = new Map();
    private currentState: BaseState | null = null;
    private targetNode: Node | null = null;

    start() {
        this.initStates();
        this.changeState('idle');
    }

    private initStates() {
        this.states.set('idle', new IdleState(this));
        this.states.set('wander', new WanderState(this));
        // 添加更多状态...
    }

    update(dt: number) {
        if (this.currentState) {
            this.currentState.update(dt);
        }
    }

    public changeState(stateName: string) {
        if (this.currentState) {
            this.currentState.exit();
        }

        const newState = this.states.get(stateName);
        if (newState) {
            this.currentState = newState;
            this.currentState.enter();
        }
    }

    public moveTowards(target: Vec3) {
        if (!this.pathFollower) return;
        this.pathFollower.setTarget(target);
    }

    public stopMoving() {
        if (!this.pathFollower) return;
        this.pathFollower.setTarget(this.node.position);
    }

    public getPosition(): Vec3 {
        return this.node.position;
    }

    public detectThreat(): boolean {
        // 实现威胁检测逻辑
        return false;
    }

    public detectFood(): boolean {
        // 实现食物检测逻辑
        return false;
    }

    public setTarget(target: Node) {
        this.targetNode = target;
    }

    public getTarget(): Node | null {
        return this.targetNode;
    }

    public attack(): void {
        // 获取目标
        const target = this.getTarget();
        if (!target) return;

        // 获取目标的可伤害组件
        interface Damageable {
            takeDamage(amount: number): void;
        }

        const damageable = target.getComponent('Damageable') as Damageable | null;
        if (damageable) {
            damageable.takeDamage(10);
        }

        // 播放攻击动画
        const animator = this.node.getComponent(Animation);
        if (animator) {
            animator.play('attack');
        }

        // 播放攻击音效
        const audioSource = this.node.getComponent(AudioSource);
        if (audioSource && this.attackSound) {
            audioSource.playOneShot(this.attackSound);
        }
    }
} 